package com.lingx.web.api.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.lingx.service.LingxService;
import com.lingx.web.api.IApi;
@Component
public class Api9018 extends AbstractDevApi implements IApi{
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private LingxService lingxService;
	@Override
	public int getApiCode() {
		return 9018;
	}
	@Override
	public String getApiName() {
		return "菜单授权";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> params) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String roleid=IApi.getParamString(params, "role_id", "");
		String menuid=IApi.getParamString(params, "menu_id", "");
		String checked=IApi.getParamString(params, "checked", "");
		String checkedSub=IApi.getParamString(params, "checkedSub", "");
		//if("true".equals(checkedSub))checkedSub=String.valueOf(this.lingxService.queryForInt("select count(*) from tlingx_menu where fid=?", menuid)>0);
		if("true".equals(checked)&&"false".equals(checkedSub)) {//||"true".equals(checkedSub)
			if(lingxService.queryForInt("select count(*) from tlingx_role_menu where role_id=? and menu_id=?", roleid,menuid)==0)
			this.jdbcTemplate.update("insert into tlingx_role_menu(id,role_id,menu_id)values(uuid(),?,?)",roleid,menuid);
			addSubAuth(menuid,roleid);
			addParentAuth(menuid, roleid);
			addFuncAuth(menuid, roleid);
		}else if("true".equals(checked)&&"true".equals(checkedSub)) {//||"true".equals(checkedSub)
			if(lingxService.queryForInt("select count(*) from tlingx_role_menu where role_id=? and menu_id=?", roleid,menuid)==0) {
			this.jdbcTemplate.update("insert into tlingx_role_menu(id,role_id,menu_id)values(uuid(),?,?)",roleid,menuid);
			addSubAuth(menuid,roleid);
			addParentAuth(menuid, roleid);
			addFuncAuth(menuid, roleid);
			}
		}else if("false".equals(checked)&&"false".equals(checkedSub)) {
			if(lingxService.queryForInt("select count(*) from tlingx_role_menu where role_id=? and menu_id=?", roleid,menuid)>0) {
			this.jdbcTemplate.update("delete from tlingx_role_menu where role_id=? and menu_id=?",roleid,menuid);
			delSubAuth(menuid,roleid);
			delParentAuth(menuid,roleid);
			delFuncAuth(menuid, roleid);
			}
		}
		return ret;
	}
	private void addParentAuth(Object id,String roleid) {
		List<Map<String,Object>> list=this.jdbcTemplate.queryForList("select fid from tlingx_menu where id=?",id);
		for(Map<String,Object> map:list) {
			if(lingxService.queryForInt("select count(*) from tlingx_role_menu where role_id=? and menu_id=?", roleid,map.get("fid"))==0)
				this.jdbcTemplate.update("insert into tlingx_role_menu(id,role_id,menu_id)values(uuid(),?,?)",roleid,map.get("fid"));
			addParentAuth(map.get("fid"),roleid);
		}
	}
	
	private void delParentAuth(Object id,String roleid) {
		List<Map<String,Object>> list=this.jdbcTemplate.queryForList("select fid from tlingx_menu where id=?",id);
		for(Map<String,Object> map:list) {
			if(lingxService.queryForInt("select count(*) from tlingx_role_menu where role_id=? and menu_id in (select id from tlingx_menu where fid=?)", roleid,map.get("fid"))==0)
				this.jdbcTemplate.update("delete from tlingx_role_menu where role_id=? and menu_id=?",roleid,map.get("fid"));
			delParentAuth(map.get("fid"),roleid);
		}
	}
	
	private void addSubAuth(Object fid,String roleid) {
		List<Map<String,Object>> list=this.jdbcTemplate.queryForList("select id from tlingx_menu where fid=?",fid);
		for(Map<String,Object> map:list) {
			if(lingxService.queryForInt("select count(*) from tlingx_role_menu where role_id=? and menu_id=?", roleid,map.get("id"))==0)
				this.jdbcTemplate.update("insert into tlingx_role_menu(id,role_id,menu_id)values(uuid(),?,?)",roleid,map.get("id"));
			addSubAuth(map.get("id"),roleid);
			addFuncAuth(map.get("id"), roleid);
		}
	}

	private void delSubAuth(Object fid,String roleid) {
		List<Map<String,Object>> list=this.jdbcTemplate.queryForList("select id from tlingx_menu where fid=?",fid);
		for(Map<String,Object> map:list) {
			this.jdbcTemplate.update("delete from tlingx_role_menu where role_id=? and menu_id=?",roleid,map.get("id"));
			delSubAuth(map.get("id"),roleid);
			delFuncAuth(map.get("id"), roleid);
		}
	}
	public static final String publicID="7903b684-7945-4b8c-b5d8-6762cbb959d7";
	private void addFuncAuth(Object menuId,String roleid) {
		List<Map<String,Object>> list=this.jdbcTemplate.queryForList("select func_id from tlingx_menu where id=? and func_id <>?",menuId,publicID);
		if(list.size()>0) {
			Object funcId=list.get(0).get("func_id");
			if(lingxService.queryForInt("select count(*) from tlingx_role_func where role_id=? and func_id=?", roleid,funcId)==0)
				this.jdbcTemplate.update("insert into tlingx_role_func(id,role_id,func_id)values(uuid(),?,?)",roleid,funcId);
			
		}
	}

	private void delFuncAuth(Object menuId,String roleid) {
		List<Map<String,Object>> list=this.jdbcTemplate.queryForList("select func_id from tlingx_menu where id=? and func_id <>?",menuId,publicID);
		if(list.size()>0) {
			Object funcId=list.get(0).get("func_id");
			this.jdbcTemplate.update("delete from tlingx_role_func where role_id=? and func_id=?",roleid,funcId);
		}
	}
}
